package com.yonyou.pmclouds.messagenotice.constructor.impl;

import com.yonyou.pmclouds.basecom.billstatus.BillStatusConst;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.entity.Entity;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.copyperson.entity.CopyPersonVO;
import com.yonyou.pmclouds.messagenotice.entity.MessageNoticeVO;
import com.yonyou.pmclouds.project.entity.ProjectVO;
import com.yonyou.pmclouds.project.rmiitf.ProjectSimpleQuery;
import com.yonyou.pmclouds.workstopreport.entity.WorkStopVO;
import com.yonyou.pmclouds.workstopreport.rmiitf.WorkStopQuery;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Component
public class WorkStopMessageConstructor extends AbstractMessageNoticeConstructor {

    private final WorkStopQuery query;

    private final ProjectSimpleQuery projectSimpleQuery;

    @Autowired
    public WorkStopMessageConstructor(WorkStopQuery query, ProjectSimpleQuery projectSimpleQuery) {
        this.query = query;
        this.projectSimpleQuery = projectSimpleQuery;
    }

    @Override
    protected MessageNoticeVO[] generateContent(Entity vo) throws BusinessException {
        MessageNoticeVO noticeVO = coverBusinessVOToMessageVO(vo);
        if(noticeVO == null){
            return null;
        }

        List<MessageNoticeVO> noticeVOList = new ArrayList<>();

        WorkStopVO report = query.getReport(noticeVO.getRelateKey());
        ProjectVO projectVO = projectSimpleQuery.queryProject(report.getPkProject());

        String prefix = "";

        //通知内容
        if(report.getBillstatus() == (BillStatusConst.COMMON_APPROVED_STATUS)) {
            prefix = APPROVAL_SUFFIX_ADOPT_MESSAGE;
        }
        if(report.getBillstatus() == (BillStatusConst.COMMON_REJECT_STATUS)) {
            prefix = APPROVAL_SUFFIX_REJECT_MESSAGE;
        }

        String messageContent = APPROVAL_PREFIX_MESSAGE + "[" +
                projectVO.getSname()
                + "]的停工单，" + prefix;

        //给制单人发送消息
        noticeVOList.add(getNoticeVO(report, projectVO.getSname(), messageContent, report.getCreator()));

        // 给抄送人发送通知
        List<CopyPersonVO> copyPersonVOS = report.getCopyPersons();
        if (CollectionUtils.isNotEmpty(copyPersonVOS)) {

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
            messageContent = report.getProjectName() + "目前已停工，停工日期为"+sdf.format(report.getStopDate())+"，预计复工日期为"+sdf.format(report.getPreresumeDate())+"请知晓";
            for (CopyPersonVO copyPersonVO : copyPersonVOS) {
                noticeVOList.add(getNoticeVO(report, projectVO.getSname(), messageContent, copyPersonVO.getCopyPerson()));
            }
        }

        return noticeVOList.toArray(new MessageNoticeVO[0]);
    }

    /**
     * 生成一条消息通知
     *
     * @param vo
     * @param title
     * @param content
     * @param receiver
     * @return
     */
    private MessageNoticeVO getNoticeVO(WorkStopVO vo, String title, String content, String receiver) {
        MessageNoticeVO noticeVO = new MessageNoticeVO();
        noticeVO.setRelateType(vo.getBillType());
        noticeVO.setRelateKey(vo.getPkWorkstopreport());
        noticeVO.setMessageTitle(title);
        noticeVO.setMessageReceiver(receiver);
        noticeVO.setPkTenant(vo.getPkTenant());
        noticeVO.setMessageContent(content);

        return noticeVO;
    }

    @Override
    public String getRelateType() {
        return BillTypeConst.WORKSTOP_BILLTYPE;
    }

}
